<template>
    <el-dialog :close-on-click-modal="false"  ref="dialogform" :title="addEditName + enunLabel + title" :visible.sync="dialogVisible" width="40%"
        :before-close="handleClose">
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="130px" class="demo-ruleForm">
            <el-form-item label="RTU(筛选数据项)" prop="rtuId">
                <el-select v-model="ruleForm.rtuId" class="w100p" clearable filterable placeholder="请选择RTU"
                    @change="queryDataItemByRtu">
                    <el-option v-for="rtu, index in RTUList" :label="rtu.rtuName" :value="rtu.rtuId + ''"
                        :key="index"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="选择数据项" prop="dataItemId">
                <el-select v-model="ruleForm.dataItemId" class="w100p" clearable filterable placeholder="请选择数据项"
                    @change="depDataItem">
                    <el-option v-for="rtu, index in dataItemList" :label="rtu.dataItemName" :value="rtu.dataItemId"
                        :key="index"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="展示数据项名称" prop="dataItemName">
                <el-input v-model="ruleForm.dataItemName" placeholder="请输入数据项名称"></el-input>
            </el-form-item>
            <el-form-item label="选择单位">
                <el-select v-model="ruleForm.unitNo" class="w100p" clearable filterable placeholder="请选择单位">
                    <el-option v-for="unit, index in unitlist" :label="`单位名称：${unit.unitName}(${unit.symbol})`"
                        :value="unit.unitNo" :key="index"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="是否展示">
                <el-switch v-model="ruleForm.visible" active-color="#13ce66" inactive-color="#ff4949" :active-value="true"
                    :inactive-value="false" active-text="是" inactive-text="否">
                </el-switch>
            </el-form-item>
            <el-form-item label="是否可控" v-if="isEnum && isEnum !== 'UNENUM'">
                <el-switch v-model="ruleForm.control" active-color="#13ce66" inactive-color="#ff4949" :active-value="true"
                    :inactive-value="false" active-text="是" inactive-text="否">
                </el-switch>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button @click="cancel">取 消</el-button>
            <el-button type="primary" @click="submitForm">确 定</el-button>
        </span>
    </el-dialog>
</template>
<script>
import dataItemManagement from '@/api/basicInformation/dataItemManagement.js'
import unitManagement from '@/api/basicInformation/unitManagement';
// import publicLnterface from '@/api/publicLnterface';

export default {
    components: {},
    props: {
        addEditFlag: {
            type: String,
            default: () => {
                return 'add'
            }
        },
        data: {
            type: Object,
            default: () => {
                return {}
            }
        },
        title: {
            type: String,
            default: () => {
                return '操作表单'
            }
        },
        dialogVisible: {
            type: Boolean,
            default: () => {
                return false
            }
        },
        category: {
            type: String,
            default: () => {
                return ''
            }
        },
        enunLabel: {
            type: String,
            default: () => {
                return ''
            }
        },
    },
    data() {
        return {
            isEnum: '',
            RTUList: [],
            dataItemList: [],
            unitlist: [],
            addEditName: '添加',
            addEditApi: dataItemManagement.addFarmingTypelist,
            ruleForm: {},
            rules: {
                dataItemId: [
                    { required: true, message: '请选择数据项', trigger: 'change' }
                ],
                dataItemName: [
                    { required: true, message: '请输入数据项名称', trigger: 'change' }
                ],
                unitNo: [
                    { required: true, message: '选择单位', trigger: 'change' }
                ]
            }
        }
    },
    computed: {

    },
    watch: {
        dialogVisible(v) {
            v && this.initData()
        }
    },
    
    mounted() {
        this.queryRtuByCompanyMark();
    },
    methods: {
        initData() {
            if (this.addEditFlag == 'add') {
                this.resetForm()
                this.addEditApi = dataItemManagement.addFarmingTypelist;
                this.addEditName = '添加';
                this.$set(this.ruleForm, 'visible', true);
                this.$set(this.ruleForm, 'control', false);
            } else if (this.addEditFlag == 'edit') {
                this.queryDataItemByRtu(this.data.rtuId);
                setTimeout(() => {
                    this.ruleForm = { ...this.data };
                    this.addEditApi = dataItemManagement.editFarmingTypelist;
                    this.addEditName = '编辑';
                }, 1000)
            }
            this.getallunitlist();
        },
        async getallunitlist() {
            // let { data } = await publicLnterface.allUnit();
            let { data } = await unitManagement.queryByCondition();
            this.unitlist = data || [];
        },
        //国家气象站Rtu
        async queryRtuByCompanyMark(mark = sessionStorage.organizationNo) {
            let { data: list1 } = await dataItemManagement.getMeteorologicalByCondition();
            let { data: list2 } = await dataItemManagement.queryRtuByCompanyMark(mark);
            this.RTUList = list2.concat(list1);
        },
        depDataItem(e) {
            let index = this.dataItemList.findIndex((b) => b.dataItemId === e)
            let form = { ...this.dataItemList[index] }
            this.$set(this.ruleForm, 'dataItemName', form.dataItemName);
            this.$set(this.ruleForm, 'dataTypeId', form.dataTypeId);
            this.$set(this.ruleForm, 'dataTypeName', form.dataType.dataTypeName);
            this.$set(this.ruleForm, 'mainingControlType', form.dataType.mainingControlType);
            this.$set(this.ruleForm, 'enumListType', form.dataType.enumListType);
            this.$set(this.ruleForm, 'originalName', form.dataItemName);
            this.isEnum = form && form.dataType.valueType ? form.dataType.valueType : '';
            if (this.isEnum == 'UNENUM') {
                this.$set(this.ruleForm, 'control', false);
            }
        },
        async getDataItemByList() {
            let { data } = await dataItemManagement.getDataItemByList(sessionStorage.organizationNo);
            this.dataItemList = data || [];
            let result = this.dataItemList.find(obj => obj.dataItemId === this.ruleForm.dataItemId);
            this.isEnum = result && result.dataType.valueType ? result.dataType.valueType : '';
            if (this.isEnum == 'UNENUM') {
                this.$set(this.ruleForm, 'control', false);
            }
        },
        async queryDataItemByRtu(rtuId, isRset = true) {
            if (isRset) {
                this.$set(this.ruleForm, 'dataItemId', '');
                this.$set(this.ruleForm, 'dataItemName', '');
                this.$set(this.ruleForm, 'originalName', '');
                this.$set(this.ruleForm, 'control', false);
                this.dataItemList = [];
            }
            if (!rtuId) return
            let { data } = await dataItemManagement.queryDataItemByRtu(rtuId);
            this.dataItemList = data || [];
        },
        //提交表单
        submitForm() {
            this.$refs.ruleForm.validate((valid) => {
                if (valid) {
                    this.addEditApi({ ...this.ruleForm, category: this.category }).then(res => {
                        this.cancel();
                        this.$message[res.code == 1 ? 'success' : 'warning'](res.code == 1 ? '操作成功' : res.msg || '操作失败')
                    }).catch(err => {
                        console.log(err)
                    })
                } else {
                    return false;
                }
            })
        },
        //重置
        resetForm() {
            this.ruleForm = {};
            this.isEnum = '';
            this.dataItemList = [];
            this.$refs.ruleForm && this.$refs.ruleForm.resetFields()
        },
        //取消关闭
        cancel() {
            this.resetForm()
            this.$emit("closeDialog")
        },
        handleClose(done) {
            this.$confirm('确认关闭？')
                .then(_ => { this.resetForm(); this.cancel() })
                .catch(_ => { });
        }
    }
};
</script>
<style lang="scss" scoped></style>
  